package com.mbmed.doctor.login.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mbmed.doctor.login.bo.PersonBo;
import com.mbmed.doctor.login.mapper.Person;

/**
 * 对人员表(person)的增删改查操作
 * 
 * @ClassName: PersonService
 * @Description: TODO(对人员表person的增删改查操作)
 * @author yangzhengxin(Jason)
 * @date 2014年2月7日 上午11:14:18
 * 
 */

@Path("/person")
@Component
public class PersonService {
	private static Logger logger = Logger.getLogger(PersonService.class);

	@Autowired
	private PersonBo personBo;

	@Context
	HttpServletRequest request;

	/**
	 * 获取所有人员信息,请求的uri：http://localhost:8080/hocrm/person
	 * 
	 * @Title: getAll
	 * @Description: TODO(获取所有人员信息,请求地址：http://localhost:8080/hocrm/person)
	 * @param
	 * @return 返回结果为Person List JSON格式
	 * @throws
	 */
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public List<Person> getAll() {
		List<Person> list = new ArrayList<Person>();
		try {
			list = personBo.getAll();
			// list.addAll(list1);
			// str = new Gson().toJson(list);
			Person p = (Person) request.getSession().getAttribute("Person");
			logger.info("==========getAll=============");
			logger.info(p);
			logger.info("==========getAll=============");
		} catch (Exception e) {
			logger.error("load all exception : ", e);
		}
		return list;
	}

	/**
	 * 获取某个人的信息,请求的uri：http://localhost:8080/hocrm/person/{id}
	 * 
	 * @Title: getPerson
	 * @Description: TODO(获取一个人员信息)
	 * @param id
	 *            主键
	 * @return 返回结果为Person JSON格式
	 * @throws
	 */
	@GET
	@Path("{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Person getPerson(@PathParam("id") java.lang.Integer id) {
		Person person = null;
		String str = null;
		try {
			person = personBo.get(id);
			Person p = (Person) request.getSession().getAttribute("Person");
			logger.info("==========getPerson=============");
			logger.info(p);
			logger.info("==========getPerson=============");
		} catch (Exception e) {
			logger.error("load a person exception : id=" + id, e);
		}
		return person;
	}

	/**
	 * 添加一个人员信息，请求的uri：http://localhost:8080/hocrm/person
	 * 
	 * @Title: addPerson
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param id
	 *            主键
	 * @param firstName
	 *            曾用名
	 * @param lastName
	 *            现用名
	 * @param city
	 *            所在地市
	 * @param address
	 *            所在地址
	 * @param servletResponse
	 * 
	 * @return 返回结果为Person JSON格式
	 * @throws
	 */
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Person addPerson(@FormParam("id") String id,
			@FormParam("firstName") String firstName,
			@FormParam("lastName") String lastName,
			@FormParam("city") String city,
			@FormParam("address") String address,
			@Context HttpServletResponse servletResponse) {
		Person p = new Person();
		if (id != null && !"".equals(id)) {
			p.setIdP(Integer.valueOf(id));
		}
		p.setFirstname(firstName);
		p.setLastname(lastName);
		p.setCity(city);
		p.setAddress(address);
		try {
			p = personBo.save(p);
			request.getSession().setAttribute("Person", p);
			Person p1 = (Person) request.getSession().getAttribute("Person");
			logger.info("===========addPerson============");
			logger.info(p1);
			logger.info("===========addPerson============");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 删除一个人员信息，请求的uri：http://localhost:8080/hocrm/person/{id}
	 * 
	 * @Title: deletePerson
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param id
	 *            主键
	 * @return void 无返回
	 * @throws
	 */
	@DELETE
	@Path("{id}")
	public void deletePerson(@PathParam("id") String id) {
		try {
			personBo.remove(Integer.valueOf(id));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @throws Exception
	 * @Title: getOnePerson
	 * @Description: 用户
	 * @return
	 * @return List<SysRole> 返回类型
	 * @throws
	 */
	@GET
	@Path("/getOnePerson")
	@Produces({MediaType.APPLICATION_JSON})
	public Person getOnePerson(
			 @QueryParam("id") Integer id //左侧树用户
			) throws Exception{
		 if(id != 0){
			 return personBo.get(id);
		 }else{
			 return null;
		 }
		
	}
}
